<HTML><HEAD><TITLE>asserta(+Clause)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Dynamic Predicates</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>asserta(+Clause)</H1>
Add specified clause for a dynamic procedure to the database before any existing clauses
<DL>
<DT><EM>Clause</EM></DT>
<DD>Atom or compound term
</DD>
</DL>
<H2>Description</H2>
   Adds the specified clause for a dynamic procedure to the database.]
   The clause is added BEFORE any existing clauses for the procedure.
<P>
   The procedure must be declared to be dynamic using the dynamic/1
   built-in.  If the procedure is undefined an exception is raised.
   However, the default error handler for this exception simply declares
   the procedure dynamic and asserts the clause.
<P>
   The asserted clause is NOT removed from the database on backtracking
   through the call to asserta/1.
<P>
   asserta/1 satisfies the logical update semantics.  Asserting a clause to
   a procedure will not, in any way, affect previous calls to it when
   backtracking.
<P>
   No clause transformation is performed on Clause. Use expand_clause/2 to
   explicitly expand the clause before calling this predicate if clause
   expansion is rquired.
<P>

<H3>Modes and Determinism</H3><UL>
<LI>asserta(+) is det
</UL>
<H3>Modules</H3>
This predicate is sensitive to its module context (tool predicate, see @/2).
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>Clause is a free variable.
<DT><EM>(4) instantiation fault </EM>
<DD>The head of Clause is a free variable.
<DT><EM>(5) type error </EM>
<DD>Clause is not a valid Prolog clause.
<DT><EM>(63) procedure not dynamic </EM>
<DD>The procedure is not dynamic.
<DT><EM>(70) accessing an undefined dynamic procedure </EM>
<DD>The procedure is undefined.  However, the default error handler for this exception simply declares the procedure dynamic and    asserts the clause, if the error was caused by an assert.
</DL>
<H2>Examples</H2>
<PRE>   Success:
    ?- asserta(city(london)), asserta(city(munich)), asserta(city(paris)).
    yes.
    ?- listing.
    city(paris).
    city(munich).
    city(london).
    yes.

    ?- asserta((likes(X,Y) :- man(X), woman(Y))).
    yes.
    ?- listing.
    likes(_186, _187) :-
        man(_186),
        woman(_187).
    yes.


   Error:
    asserta(X).                        - gives error 4.
    asserta("the man").                - gives error 5.
    asserta(1).                        - gives error 5.
    asserta((my_static(X):-write(X))). - gives error 63.
                                        if my_static/1 is not
                                        dynamic.
   Logical semantics :

   If the following clauses are in the database :
p :- asserta(p), fail.
p :- fail.

q :- fail.
q :- asserta(q), fail.

   The queries p.  and q.  will both fail.
</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/dynamic/dynamic-1.html">dynamic / 1</A>, <A HREF="../../kernel/dynamic/assert-1.html">assert / 1</A>, <A HREF="../../kernel/dynamic/assertz-1.html">assertz / 1</A>, <A HREF="../../kernel/dynamic/retract-1.html">retract / 1</A>, <A HREF="../../kernel/compiler/compile_term-1.html">compile_term / 1</A>
</BODY></HTML>
